#!/usr/local/python27/bin/python
# coding=utf8
'''
Created on May 14, 2014

@author: jin
'''

__version__ = "0.0.1#date:2014-05-10"


__all__ = ['cleanEnv',
    ]

import os
import subprocess
import ConfigParser
import logging
import time,datetime
import socket
import httplib
from HTMLParser import HTMLParser
import sqlite3
import smtplib  
from email.MIMEMultipart import MIMEMultipart  
from email.MIMEText import MIMEText
from email.utils import COMMASPACE
import sys
reload(sys)
sys.setdefaultencoding('utf8')

#You can config here
Maxretrytime = 2
Maxlevel = 3
Wtimeout = 10
badstatus = ['400', '401', '403', '404', '405', '406', '407', '408', '409', '410', '411', '412', '413', '414', '415', '416', '417', '500', '501', '502', '502', '503', '504', '505']
mediastype = ['jpg','jpeg','png','bnp','gif','ico','svg','flv']
resourcetype = ['link','media','script','css']

#mail
smtpServer = '127.0.0.1'
smtpPort = '25'
userName = 'foxdiege'
passWord = 'foxdiegepassword'
sender = 'foxdiege@gmail.com'
receiver = ['foxdiege@gmail.com','foxdiege@gmail.com']
ccuser = 'foxdiege@gmail.com' 


progName = 'httpappmonitor'
pwdDir   = os.getcwd()
workDir  = os.path.dirname(sys.argv[0])

configFile = progName+'.ini'
todayDate  = time.strftime("%Y%m%d", time.localtime())
nowtime = datetime.datetime.now()
yestime = nowtime + datetime.timedelta(days=-1)
yesDate = yestime.strftime('%Y%m%d')

if workDir ==  '' or workDir == '.':
    progDir = pwdDir
elif workDir.startswith('/'):
    progDir = workDir
elif workDir.startswith('./'):
    progDir = pwdDir+workDir.lstrip('.')
else:
    progDir = pwdDir+'/'+workDir

confPath  = progDir+'/'+configFile
logPath   = progDir+'/'+progName+'.log'
excludedomainfile = progDir+'/excludedomain.txt'
dbPath = progDir+'/'+'urlstat.db'

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)-8s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    filename=logPath,
                    filemode='a')
console = logging.StreamHandler()
console.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
      
class MyHTMLParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.links = []
        self.medias = []
        self.scripts = []
        self.csss = []
    def handle_starttag(self, tag, attrs):
        if tag == "a":
            if len(attrs) != 0:
                for (variable, value)  in attrs:
                    if variable == "href":
                        self.links.append(value)
        elif tag == "img":
            if len(attrs) != 0:
                for (variable, value)  in attrs:
                    if variable == "src":
                        self.medias.append(value)
        elif tag == "script":
            if len(attrs) != 0:
                for (variable, value)  in attrs:
                    if variable == "src":
                        self.scripts.append(value)
        elif tag == "link":
            if len(attrs) != 0:
                for (variable, value)  in attrs:
                    if variable == "href":
                        if value.split('.')[-1] in mediastype:
                            self.medias.append(value)
                        elif value.endswith('.css'):
                            self.csss.append(value)
                        else:
                            self.links.append(value)                       
    def get_data(self,tagtype):
        if tagtype == 'link':
            return self.links
        elif tagtype == 'media':
            return self.medias
        elif tagtype == 'script':
            return self.scripts
        elif tagtype == 'css':
            return self.csss
            
def cleanEnv(dbpath,filename,tablename):
    '''clean old table and report file'''
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (dbpath,filename,tablename)
    logging.info("%s: Input %s" % (Call,inputInfo))
    dropTAB= 'DROP table if exists '+tablename
    try:
        if os.path.isfile(filename):
            os.remove(filename)
        elif os.path.isdir(filename):
            os.removedirs(filename)
        conn = sqlite3.connect(dbpath)
	conn.text_factory = str
        logging.info("%s: Execute SQL <%s>" % (Call,dropTAB))
        cursor = conn.execute(dropTAB)
        conn.commit()
        conn.close()
    except Exception,e:
        logging.error("%s:Fail exit with reason<%s>!" % (Call,e))
        return False
    else:
        logging.info("%s: Sucessful" % (Call))
        return True
        
def initDB(dbpath,tablename):
    '''init db create table'''
    Call = 'Call '+sys._getframe().f_code.co_name
    #createTAB='create table if not exists '+tablename+'(id INTEGER not null primary key autoincrement,url varchar(128) not null,status varchar(25) not null,type varchar(10) not null,costtime smallint,level smallint,retry smallint default 0,source varchar(128),UNIQUE(url))'
    createTAB='create table if not exists '+tablename+'(id INTEGER not null primary key autoincrement,url varchar(128) not null,status varchar(25) not null,type varchar(10) not null,costtime smallint,level smallint,retry smallint default 0,source varchar(128))'
    dropTAB= 'DROP table if exists '+tablename
    inputInfo = (dbpath,tablename)
    logging.info("%s: Input %s" % (Call,inputInfo))
    try:
        conn = sqlite3.connect(dbpath)
	conn.text_factory = str
        logging.info("%s: Execute SQL <%s>" % (Call,dropTAB))
        cursor = conn.execute(dropTAB)
        conn.commit()
        logging.info("%s: Execute SQL <%s>" % (Call,createTAB))
        cursor = conn.execute(createTAB)
        conn.commit()
        conn.close()
    except Exception,e:
        logging.error("%s:Fail exit with reason<%s>!" % (Call,e))
        return False
    else:
        logging.info("%s: Sucessful" % (Call))
        return True
           
def sendemail(smptServer,smtpPort,from_addr,to_addr_list,cc_addr_list,subject,username,password,msg_content):
    '''sendmail'''
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (smptServer,smtpPort,from_addr,to_addr_list,cc_addr_list,subject,username,password,msg_content)
    logging.info("%s: Input %s" % (Call,inputInfo))
    msg = MIMEMultipart()
    msg['From'] = from_addr
    msg['To'] = COMMASPACE.join(to_addr_list)
    msg['Cc'] = cc_addr_list
    msg['Subject'] = subject
    msg.attach(MIMEText(msg_content, 'html'))
    smtp = smtplib.SMTP()
    try:
        smtp.connect(host=smptServer,port=smtpPort)
        smtp.login(username,password)
        content = msg.as_string()
        smtp.sendmail(from_addr,to_addr_list,content)
    except Exception,e:
        logging.error("%s:Fail exit with reason<%s>!" % (Call,e))
        print "sendemail error!"
        return False
    else:
        smtp.quit()
        return True
        
def urlComb(url):
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (url)
    logging.info("%s: Input %s" % (Call,inputInfo))
    L = url.lstrip("http:").split('/')
    domain = L[2]
    suffix = '/'+'/'.join(L[3:])
    logging.info("%s: Output %s" % (Call,(domain,suffix)))
    return (domain,suffix)

def isGbdomain(host):
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (host)
    logging.info("%s: Input %s" % (Call,inputInfo))
    filename=excludedomainfile
    with open(filename) as f:
        gbdomainlist= [i.strip('\n') for i in f.readlines()]
    if host.endswith('com.cn') or host.endswith('com.hk'):
        host='.'.join(host.split('.')[-3:])
    elif host.endswith('com') or host.endswith('org')  or host.endswith('net') or host.endswith('cn') or host.endswith('jp'):
        host='.'.join(host.split('.')[-2:])     
    if host in gbdomainlist:
        logging.info("%s: %s is GB domain." % (Call,host))
        return True
    else:
        logging.info("%s: %s is not GB domain,skip it" % (Call,host))
        return False

def tabURL(host,urls):
    urls = [i.strip() for i in urls]
    newurls = []
    delurls = []
    for i in urls: 
        if i.startswith('java') or i.startswith('#') or i.startswith('//')  or  i.startswith('mailto:') or i == '/':
            delurls.append(i)    
    for i in delurls:
        urls.remove(i)
    for i in urls:
        if not i.startswith('http'):
            if i.startswith('/'):
                replacelink = 'http://'+host+i
            else:
                replacelink = 'http://'+host+'/'+i
            newurls.append(replacelink)
        else:
            newurls.append(i)
    return newurls

def GetSingleUrldata(domain,suffix,port=80,strict=False,timeout=30):
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (domain,suffix)
    logging.info("%s: Input %s" % (Call,inputInfo))
    url = 'http://'+domain+suffix
    data = {}
    conn = httplib.HTTPConnection(domain,port,strict,timeout) 
    try:
        starttime = time.strftime("%s", time.localtime())
        conn.request('GET',suffix)
        response = conn.getresponse()
    except Exception, e:
        data['content'] = ''
        data['status'] = 'response'
        logging.error("%s: %s  %s response Fail exit with reason<%s>!" % (Call,domain,suffix,e))
    else:
        logging.info("%s: %s %s response Sucessful" % (Call,domain,suffix))
        data['content'] = response.read()
        data['status'] = str(response.status)
    finally:
        conn.close()
        endtime = time.strftime("%s", time.localtime())
        costtime = int(endtime)-int(starttime)  
        data['url'] = url 
        data['costtime'] = costtime
        logging.info("%s: return <%s> data" % (Call,url))
        return  data
     
def GetUrlData(selfurl,htmlcode,types):
    '''get a url define dict from the url htmlcod'''
    Call = 'Call '+sys._getframe().f_code.co_name
    #inputInfo = (selfurl,htmlcode,types)
    inputInfo = (selfurl,types)
    logging.info("%s: Input %s" % (Call,inputInfo))
    host = urlComb(selfurl)[0]
    fact = MyHTMLParser()
    fact.feed(htmlcode)
    fact.close()
    data = {'selfurl':selfurl}
    for i in types:
        urllist = fact.get_data(i)
        urllist = tabURL(host,urllist)
        data[i] = {}.fromkeys(urllist).keys()
    return data

def GetUrlStatData(data,level,port=80,strict=False,timeout=30):
    '''get a level url stat data,the data will insert into dms'''
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (data,level,strict,timeout)
    logging.info("%s: Input %s" % (Call,inputInfo))
    UrlStatData = []
    sourceurl = data['selfurl']
    try:
        for k in data:
            if k == 'selfurl':
                pass
            else:
                for i in data[k]:
                    logging.info("%s: Check type <%s> Begin" % (Call,k))
                    T = urlComb(i)
                    domain = T[0]
                    suffix = T[1]
                    if isGbdomain(domain):
                        if i not in str(hasdoneurls):
                            logging.info("%s: Check url <%s> Begin" % (Call,i))
                            info = GetSingleUrldata(domain=domain,suffix=suffix,port=port,strict=strict,timeout=timeout)
                            if info['status']:
                                hasdoneurls.append(i)
                                singledata= (i,info['status'],k,info['costtime'],level,sourceurl)
                                UrlStatData.append(singledata)
                            else:
                                print "ERROR no data return please check!!!"
                                sys.exit(1)
                        else:
                            logging.info("%s: %s aready check,skip it" % (Call,i))
                            print "%s aready check,skip it" % i
                    
    except Exception, e:
        logging.error("%s: On Layer<%s> error with reason<%s>!" % (Call,level,e))
    else:
        logging.info("%s: On Layer<%s> Sucessful" % (Call,level))
	UrlStatData={}.fromkeys(UrlStatData).keys()
        return UrlStatData
       
def retryCheck(urlid,url,status,curretrytime,maxretrytime=Maxretrytime):
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (urlid,url,status,curretrytime,maxretrytime)
    logging.info("%s: Input %s" % (Call,inputInfo))
    while curretrytime < maxretrytime:
        curretrytime+=1
        logging.error("%s: Check url <%s> retry %d Begin" % (Call,url,curretrytime))
        T = urlComb(url)
        domain = T[0]
        suffix = T[1]
        info = GetSingleUrldata(domain=domain,suffix=suffix)
        if info['status']==status:
            updateSQL='update '+tablename+' set retry='+str(curretrytime)+' where id='+str(urlid)
        else:
            updateSQL='update '+tablename+' set retry='+str(curretrytime)+' and status='+info['status']+' where id='+str(urlid)
        conn = sqlite3.connect(dbPath)
	conn.text_factory = str
        cursor = conn.cursor()
        logging.info("%s: Execute SQL <%s> Begin" % (Call,updateSQL))        
        cursor.execute(updateSQL)
        conn.commit()
        cursor.close()
        conn.close()
        retryCheck(url,status,curretrytime,maxretrytime)      
  
def retryCheckLayer(dbPath,level=1):
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (dbPath,level)
    logging.info("%s: Input %s" % (Call,inputInfo))
    selectSQL='select * from '+tablename+' where level='+str(level)
    conn = sqlite3.connect(dbPath)
    conn.text_factory = str
    cursor = conn.cursor()
    logging.info("%s: execute SQL <%s> Begin" % (Call,selectSQL))
    cursor.execute(selectSQL)
    conn.commit()
    data=cursor.fetchall()
    cursor.close()
    conn.close()
    for i in data:
        urlid=i[0]
        fullurl=i[1]
        status = i[2]
        level = i[5]
        retrytime = i[6]
        if status == '302':
            logging.info("%s: Need retry data (level:<%d> id:<%s> url:<%s> status:<%s> retrytime:<%d>)" % (Call,level,urlid,fullurl,status,retrytime))
            print "%s: Need retry data (level:<%d> id:<%s> url:<%s> status:<%s> retrytime:<%d>)" % (Call,level,urlid,fullurl,status,retrytime)
            retryCheck(urlid,fullurl,status,retrytime)
        else:
            logging.info("%s: Not Need retry url:<%s> status:<%s> skip it" % (Call,fullurl,status))
    return True
            
def Entrance(host,port=80,url='/',strict=False,timeout=10):
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (host,port,url,strict,timeout)
    logging.info("%s: Input %s" % (Call,inputInfo))
    level = 0
    global topfullurl
    topfullurl = 'http://'+host+url
    info = GetSingleUrldata(domain=host,suffix=url,port=port,strict=strict,timeout=timeout)
    if info:
        hasdoneurls.append(topfullurl)
        EntranceData="'"+topfullurl+"'"+','+info['status']+','+"'link'"+','+str(level)+','+str(info['costtime'])+','+"'top'"
        SQL='insert into '+tablename+' (url,status,type,costtime,level,source) values ('+EntranceData+')'
        conn = sqlite3.connect(dbPath)
	conn.text_factory = str
        cursor = conn.cursor()
        logging.info("%s: execute SQL <%s>" % (Call,SQL))
        cursor.execute(SQL)
        conn.commit()
	if info.has_key('content'):
            urldata = GetUrlData(topfullurl,info['content'],resourcetype)
            if urldata:
                level+=1
                UrlStatData=GetUrlStatData(urldata,level)
                if UrlStatData:
                    SQL = 'insert into '+tablename+' (url,status,type,costtime,level,source) values (?,?,?,?,?,?)'
                    cursor.executemany(SQL, UrlStatData)
                    conn.commit()
                    cursor.close()
                    conn.close()
                    retryCheckLayer(dbPath,level)
                    WorkLayer(dbPath,level)
                    return True
        else:
            logging.error("%s: %s has not htmlcode!" % (Call,topfullurl))
    else:
        logging.error("%s: %s % return info fail!" % (Call,topfullurl))        
  
def WorkLayer(dbPath,level=1):
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (dbPath,level)
    logging.info("%s: Input %s" % (Call,inputInfo))
    selectSQL='select * from '+tablename+' where level='+str(level)
    conn = sqlite3.connect(dbPath)
    conn.text_factory = str
    cursor = conn.cursor()
    logging.info("%s: execute SQL <%s> Begin" % (Call,selectSQL))
    cursor.execute(selectSQL)
    conn.commit()
    data=cursor.fetchall()
    cursor.close()
    conn.close()
    while level < Maxlevel:
    	level+=1
        for i in data:
            if i[2]== '200' and i[3]== 'link':
                url=i[1]
                T = urlComb(url)
                domain = T[0]
                suffix = T[1]
                info = GetSingleUrldata(domain=domain,suffix=suffix)
		if info.has_key('content'):
	            #add debug
		    try:
                    	urldata = GetUrlData(url,info['content'],resourcetype)
    		    except Exception, e:
			print printtime('%Y-%m-%d %H:%M:%S'),"Error: GetUrlData error,Please check it!"
        		logging.error("%s: info content <%s>" %  (Call,info['content']))
		    else:
                    	if urldata:
                            UrlStatData=GetUrlStatData(urldata,level)
                            if UrlStatData:
                                SQL = 'insert into '+tablename+' (url,status,type,costtime,level,source) values (?,?,?,?,?,?)'
                                conn = sqlite3.connect(dbPath)
				conn.text_factory = str
                                cursor = conn.cursor()
                                cursor.executemany(SQL, UrlStatData)
                                conn.commit()
                                cursor.close()
                                conn.close()
                                retryCheckLayer(dbPath,level)
        WorkLayer(dbPath,level)
 
def getLevekWarningdata(dbPath,level=1,timeout=Wtimeout):
    '''get a level warning data'''
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (dbPath,level)
    logging.info("%s: Input %s" % (Call,inputInfo))
    selectSQL='select * from '+tablename+' where level='+str(level)
 
    conn = sqlite3.connect(dbPath)
    conn.text_factory = str
    cursor = conn.cursor()
    logging.info("%s: execute SQL <%s> Begin" % (Call,selectSQL))
    cursor.execute(selectSQL)
    conn.commit()
    data=cursor.fetchall()
    cursor.close()
    conn.close()
    warningdatas=[]
    for i in data:
        if i[2] in badstatus or i[4] > int(timeout):
            if i[4] > 10:
                urlstatus = 'timeout'
            else:
                urlstatus = i[2]
            urlid=i[0]
            urlname = i[1]
            urltype = i[3]
            urllevel = i[5]
            urlsource = i[7]
            urlstatline = ' '*6+'+- '+urlname+' '+urlstatus+' '+urltype+'\n'
            warningdata =  (urlid,urlname,urlstatus,level,urlsource)
            warningdatas.append(warningdata)
    return warningdatas

def getAllWarningdata(dbPath,maxlevel=4):
    '''get all level warning data,you can select from dms get maxlevel+'''
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (dbPath,maxlevel)
    logging.info("%s: Input %s" % (Call,inputInfo))
    allwarningdatas=[]
    for i in range(0,int(maxlevel)):
        allwarningdatas.append(getLevekWarningdata(dbPath,i))
    return allwarningdatas
 
def makeReport(data,reportfile):
    datalist=[]
    for l in data:
        for u in l:
            url=u[1]
            status=u[2]
            level=u[3]
            source=u[4]
            if level==3:
                SQL="select source,url from "+tablename+" where url=(select url from "+tablename+" where url=(select source from "+tablename+" where url='"+url+"'))"
                conn = sqlite3.connect(dbPath)
		conn.text_factory = str
                cursor = conn.cursor()
                cursor.execute(SQL)
                conn.commit()
                data = cursor.fetchall()
                linedata = [topfullurl,data[0][0],data[0][1],url,status]
                datalist.append(linedata)
                cursor.close()
                conn.close() 
            elif level==2:
                SQL="select source,url from "+tablename+" where url=(select url from "+tablename+" where url='"+url+"')"
                conn = sqlite3.connect(dbPath)
		conn.text_factory = str
                cursor = conn.cursor()
                cursor.execute(SQL)
                conn.commit()
                data = cursor.fetchall()
                linedata = [topfullurl,data[0][0],url,'',status]
                datalist.append(linedata)
                cursor.close()
                conn.close()                 
            elif level==1:
                linedata = [source,url,'','',status,level]
                datalist.append(linedata)
            elif level==0:
                linedata = [url,'','','',status,level]
                datalist.append(linedata)
            else:
                print "level %d not define!" % level
    toplist=[]
    seclist=[]
    thrlist=[]
    newdata=[]
    newdata.append('<table border="1">')
    tiledata='<tr align="center"><td><b>Top</b></td><td><b>Level 1</b></td><td><b>Leve 2</b></td><td><b>Level 3</b></td><td><b>Status</b></td></tr>'+"\n"
    newdata.append(tiledata)
    for i in datalist:
        if i[0] in toplist:
            del i[0]
            i.insert(0,'+')
            if i[1] in seclist:
                del i[1]
                i.insert(1,'+')
                if i[2] in thrlist:
                    del i[2]
                    i.insert(2,'+')
                else:
                    thrlist.append(i[2])
            else:
                seclist.append(i[1])
                if i[2] in thrlist:
                    del i[2]
                    i.insert(2,'+')
                else:
                    thrlist.append(i[2])
        else:
            toplist.append(i[0])
            if i[1] in seclist:
                del i[1]
                i.insert(1,'+')
                if i[2] in thrlist:
                    del i[2]
                    i.insert(2,'+')
                else:
                    thrlist.append(i[2])
            else:
                seclist.append(i[1])
                if i[2] in thrlist:
                    del i[2]
                    i.insert(2,'+')
                else:
                    thrlist.append(i[2])
        linedata=  '<tr><td>'+i[0]+'</td><td>'+i[1]+'</td><td>'+i[2]+'</td><td>'+i[3]+'</td><td>'+i[4]+'</td></tr>'+"\n"
        newdata.append(linedata)
    newdata.append('</table>')
    try:
        with open(reportfile,'wt') as f:
            f.writelines(newdata)
    except Exception, e:
        print 'make report error'
    else:
        return reportfile
        
def sendReport(reportfile):
    '''report from report.html'''
    Call = 'Call '+sys._getframe().f_code.co_name
    inputInfo = (reportfile)
    logging.info("%s: Input %s" % (Call,inputInfo))   
    subject = "%s report %s" % (site,todayDate)
    if reportfile:
        with open(reportfile,'rt') as f:
            lines=f.readlines()
        if len(lines) > 2:
            with open(reportfile,'rt') as f:
                content=f.read()
            sendemail(smtpServer,smtpPort,sender,receiver,ccuser,subject,userName,passWord,content)
	    #if send mail ok report.html will delete
        else:
            print 'No warning!'
def getleveldata(dbPath,level='all'):
    '''get level data'''
    if level=='all':
        selectSQL='select * from '+ tablename
    else:
        selectSQL='select * from '+ tablename+' where level='+str(level)
    conn = sqlite3.connect(dbPath)
    conn.text_factory = str
    cursor = conn.cursor()
    cursor.execute(selectSQL)
    conn.commit()
    data = cursor.fetchall()
    cursor.close()
    conn.close()
    for i in data:
        urlid=i[0]
        fullurl=i[1]
        stat = i[2]
        level = i[5]
        print "level:<%d> url:<%s> status:<%s>" % (level,fullurl,stat)
        
def getstatudata(dbPath,status='all'):
    '''get level data'''
    if status=='all':
        selectSQL='select * from '+ tablename
    else:
        selectSQL='select * from '+ tablename+' where status='+str(status)
    conn = sqlite3.connect(dbPath)
    conn.text_factory = str
    cursor = conn.cursor()
    cursor.execute(selectSQL)
    conn.commit()
    data = cursor.fetchall()
    cursor.close()
    conn.close()
    for i in data:
        urlid=i[0]
        fullurl=i[1]
        stat = i[2]
        level = i[5]
        print "level:<%d> url:<%s> status:<%s>" % (level,fullurl,stat)
             
def main():
    global hasdoneurls
    hasdoneurls = []
    if len(sys.argv) >= 3:
        opt=sys.argv[2]
        global site
        site = sys.argv[1]
        global tablename
        global cleantablename
        tablename='_'.join(site.split('.'))+'_'+todayDate
        cleantablename='_'.join(site.split('.'))+'_'+yesDate
        global reportfilename
        global cleanreportfilename
        reportfilename=site+'-'+todayDate+'.html'
        cleanreportfilename=site+'-'+yesDate+'.html'
    else:
        print 'Usage: %s sitename action argv\nFor Example:\n%s www.haohaizi.com monitor\n%s www.haohaizi.com level [all|1|2|3]\n%s www.haohaizi.com status [all|404|500|302]' % (sys.argv[0],sys.argv[0],sys.argv[0],sys.argv[0])
        sys.exit(1)

    if opt=='monitor':
        BeginTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        print "Begin at %s" % BeginTime
        if cleanEnv(dbPath,cleanreportfilename,cleantablename):
            if initDB(dbPath,tablename):
                if Entrance(site):
                    reportfile=makeReport(getAllWarningdata(dbPath),reportfilename)
                    if reportfile:
                        sendReport(reportfile)
        EndTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        print "End at %s" % EndTime
    elif opt=='level' and len(sys.argv)==4:
        level=sys.argv[3]
        getleveldata(dbPath,level)
    elif opt=='status' and len(sys.argv)==4:
        status=sys.argv[3]
        getstatudata(dbPath,status)
    else:
        print 'Usage: %s sitename action argv\nFor Example:\n%s www.haohaizi.com monitor\n%s www.haohaizi.com level [all|1|2|3]\n%s www.haohaizi.com status [all|404|500|302]' % (sys.argv[0],sys.argv[0],sys.argv[0],sys.argv[0])
        sys.exit(1)   
    
if __name__ == '__main__':
     main()
